package com.sinosoft.ptas.control;

import java.util.ArrayList;
import java.util.List;




import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinosoft.framework.core.common.exception.BusinessException;
import com.sinosoft.framework.core.common.utils.StringUtil;
import com.sinosoft.framework.core.common.utils.ajax.ResponseData;
import com.sinosoft.ptas.control.dto.OrganizationData;
import com.sinosoft.ptas.control.dto.TargetAttributeData;
import com.sinosoft.ptas.entity.TargetAttOrgRefEntity;
import com.sinosoft.ptas.entity.TargetAttributeEntity;
import com.sinosoft.ptas.service.TargetAttributeService;
import com.sinosoft.ptas.system.commom.CommonConstant;
import com.sinosoft.ptas.system.commom.ExceptionConstant;
/**
 * 目标属性控制层
 * @author lianWuJun
 * @time   2017年2月28日 上午11:42:17
 */
@Controller
@RequestMapping(value="targetAttribute")
public class TargetAttributeController {

	@Autowired
	TargetAttributeService targetAttributeService;
	
	/**
	 * 创建目标属性
	 * @param targetAttributeData
	 * @return 目标属性传输类
	 */
	@RequestMapping(value="/createTargetAttribute.service", method=RequestMethod.POST)
	@ResponseBody
	public ResponseData createTargetAttribute(TargetAttributeData targetAttributeData){
		if(targetAttributeData==null){
			return new ResponseData(false,ExceptionConstant.DATA_NULL_CODE,ExceptionConstant.DATA_NULL_MESSAGE,null);
		}
		try{
			TargetAttributeEntity targetAttributeEntity=targetAttributeData.toEntity();
			String id=targetAttributeService.createTargetAttribute(targetAttributeEntity);
			TargetAttributeEntity tarA=new TargetAttributeEntity();
			targetAttributeData.setId(id);
			tarA.setId(id);
			targetAttributeData.setOname(targetAttributeService.retrieveTargetAttributes(tarA).get(0).getOname());
			targetAttributeData.setPcname(targetAttributeService.retrieveTargetAttributes(tarA).get(0).getPcname());
			return new ResponseData(true,targetAttributeData);
		}catch(BusinessException e){
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),false);	
		}
	}
	
	/**
	 * 根据目标属性编号查询目标属性
	 * @param id
	 * @return 目标属性传输类
	 */
	@RequestMapping(value="/retrieveTargetAttributeById.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveTargetAttributeById(String id){
		if(StringUtil.isBlank(id)){
			return new ResponseData(false,ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE,null);
		}
		TargetAttributeEntity targetAttributeEntity=targetAttributeService.retrieveTargetAttributeById(id);
		TargetAttributeData targetAttributeData=toTargetAttributeData(targetAttributeEntity);
		return new ResponseData(true,targetAttributeData);
	}
	
	/**
	 * 根据目标类的属性查询目标属性
	 * @param targetAttributeData
	 * @return 目标传输类集合
	 */
	@RequestMapping(value="/retrieveTargetAttributes.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveTargetAttributes(TargetAttributeData targetAttributeData){
		TargetAttributeEntity targetAttributeEntity=targetAttributeData.toEntity();
		List<TargetAttributeEntity> TargetAttributeEntitys=targetAttributeService.retrieveTargetAttributes(targetAttributeEntity);
		List<TargetAttributeData> TargetAttributeDatas=getTargetAttributeDatas(TargetAttributeEntitys);
		return new ResponseData(true,TargetAttributeDatas);	
	}
	
	/**
	 * 更新目标属性
	 * @param targetAttributeData
	 * @return 目标属性传输类
	 */
	@RequestMapping(value="/updateTargetAttribute.service", method=RequestMethod.POST)
	@ResponseBody
	public ResponseData updateTargetAttribute(TargetAttributeData targetAttributeData){
		if(targetAttributeData==null){
			return new ResponseData(false,ExceptionConstant.DATA_NULL_CODE,ExceptionConstant.DATA_NULL_MESSAGE,null);
		}
		try{
			TargetAttributeEntity targetAttributeEntity=targetAttributeData.toEntity();
			targetAttributeService.updateTargetAttribute(targetAttributeEntity);
			
			return new ResponseData(true,targetAttributeData);
		}catch(BusinessException e){
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),false);	
		}
	}
	
	/**
	 * 更新目标属性(适用于添加项目种类表以后的更新)
	 * @param targetAttributeData
	 * @return 目标属性传输类
	 */
	@RequestMapping(value="/updateTargetAttributepc.service", method=RequestMethod.POST)
	@ResponseBody
	public ResponseData updateTargetAttributepc(TargetAttributeData targetAttributeData){
		if(targetAttributeData==null){
			return new ResponseData(false,ExceptionConstant.DATA_NULL_CODE,ExceptionConstant.DATA_NULL_MESSAGE,null);
		}
		try{
			String id=targetAttributeData.getId();
			if(targetAttributeService.retieveTargetAttributeOrganizationRef(id)==null){
				TargetAttOrgRefEntity TargetAttOrgRefEntity=new TargetAttOrgRefEntity();
				TargetAttOrgRefEntity.setId(UUID.randomUUID().toString());
				TargetAttOrgRefEntity.setOid(targetAttributeData.getOid());
				TargetAttOrgRefEntity.setTaid(id);
				targetAttributeService.createTargetAttributeOrganizationRefupdate(TargetAttOrgRefEntity);
			}
			
			TargetAttributeEntity targetAttributeEntity=targetAttributeData.toEntity();
			targetAttributeService.updateTargetAttributepc(targetAttributeEntity);
			
			TargetAttributeEntity tarA=new TargetAttributeEntity();
			targetAttributeData.setId(id);
			tarA.setId(id);
			targetAttributeData.setOname(targetAttributeService.retrieveTargetAttributes(tarA).get(0).getOname());
			targetAttributeData.setPcname(targetAttributeService.retrieveTargetAttributes(tarA).get(0).getPcname());
			return new ResponseData(true,targetAttributeData);
		}catch(BusinessException e){
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),false);	
		}
	}
	
	/**
	 * 根据目标属性编号删除目标属性
	 * @param id
	 */
	@RequestMapping(value="/deleteTargetAttributeById.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData deleteTargetAttributeById(String id){
		if(StringUtil.isBlank(id)){
			return new ResponseData(false,ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE,null);
		}
		try{
			targetAttributeService.deleteTargetAttributeById(id);
			return new ResponseData(true,CommonConstant.DELETE_SUCCESS);
		}catch(BusinessException e){
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),false);	
		}			
	}
	
	/**
	 * 根据其他司编号查询目标属性
	 * @param orgId
	 * @return 目标属性传输类集合
	 */
	@RequestMapping(value="/retrieveTargetAttributeByOrgId.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveTargetAttributeByOrgId(String orgId){
		if(StringUtil.isBlank(orgId)){
			return new ResponseData(false,ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE,null);
		}
		try{
			List<TargetAttributeEntity> targetAttributeEntitys=targetAttributeService.retrieveTargetAttributeByOrgId(orgId);
			List<TargetAttributeData> targetAttributeDatas=getTargetAttributeDatas(targetAttributeEntitys);
			return new ResponseData(true,targetAttributeDatas);
		}catch(BusinessException e){
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),false);	
		}
	}
	
	/**
	 * 根据目标属性编号查询司局
	 * @param taId
	 * @param identify   标识  0代表省司  1代表其他司
	 * @return 其他司传输类集合
	 */
	@RequestMapping(value="/retrieveOrganizationByTaId.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveOrganizationByTaId(String taId){
		if(StringUtil.isBlank(taId)){
			return new ResponseData(false,ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE,null);
		}
		try{
			String identify="";
			List<OrganizationData> organizationDatas=targetAttributeService.retrieveOrganizationByTaId(taId, identify);
			return new ResponseData(true,organizationDatas);
		}catch(BusinessException e){
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),false);	
		}
	}
	
	/**
	 * 根据工程类别编号查询目标属性
	 * @param pcid
	 * @return 目标属性传输类集合
	 */
	@RequestMapping(value="/retrieveTargetAttributeBypcid.service",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData retrieveTargetAttributeBypcid(String pcid){
		if(StringUtil.isBlank(pcid)){
			return new ResponseData(false,ExceptionConstant.ID_NULL_CODE,ExceptionConstant.ID_NULL_MESSAGE,null);
		}
		try{
			List<TargetAttributeEntity> targetAttributeEntitys=targetAttributeService.retrieveTargetAttributeBypcid(pcid);
			List<TargetAttributeData> targetAttributeDatas=getTargetAttributeDatas(targetAttributeEntitys);
			return new ResponseData(true,targetAttributeDatas);
		}catch(BusinessException e){
			return new ResponseData(false,e.getErrorCode(),e.getMessage(),false);	
		}
	}
	
	/**
	 * @param targetAttributeEntity
	 * @return 目标属性实体类转化为传输类
	 */
	private TargetAttributeData toTargetAttributeData(TargetAttributeEntity targetAttributeEntity) {
		TargetAttributeData targetAttributeData = new TargetAttributeData();
		targetAttributeData.toTargetAttributeData(targetAttributeEntity);
		return targetAttributeData;
	}
	
	/**
	 * 目标属性实体类集合转化为传输类集合
	 * @param targetAttributeEntitys
	 * @return 目标属性实体传输类
	 */
	private List<TargetAttributeData> getTargetAttributeDatas(List<TargetAttributeEntity> targetAttributeEntitys) {
		int size = targetAttributeEntitys.size();
		List<TargetAttributeData> targetAttributeDatas = new ArrayList<TargetAttributeData>();
		for (int i = 0; i < size; i++) { // 循环将实体数据转换为传输数据
			TargetAttributeData targetAttributeData = new TargetAttributeData();
			targetAttributeData.toTargetAttributeData(targetAttributeEntitys.get(i));
			targetAttributeDatas.add(targetAttributeData);
		}
		return targetAttributeDatas;
	}
	
}
